package top.hidoctor.auscultation.feature.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;


import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.gesture.ContainerScrollType;
import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.util.ChartUtils;
import lecho.lib.hellocharts.view.LineChartView;
import top.hidoctor.auscultation.R;
import top.hidoctor.auscultation.adapter.RankAdapter;
import top.hidoctor.auscultation.base.BaseActivity;
import top.hidoctor.auscultation.bean.StatisticsTabEnitty;
import top.hidoctor.auscultation.widget.chinaMap.ChinaMapView;

public class StatisticsActivity extends BaseActivity {

    private RecyclerView rcBiaozhu, rcBingzhong;
    List<String> list = new ArrayList<>();
    ArrayList<CustomTabEntity> tabList = new ArrayList<>();
    private CommonTabLayout tab;
    private SegmentTabLayout tab2;
    private LineChartView chart;
    private ChinaMapView chinaMap;
    private int numberOfLines = 1;
    private int maxNumberOfLines = 4;
    private int numberOfPoints = 12;
    float[][] randomNumbersTab = new float[maxNumberOfLines][numberOfPoints];
    private boolean hasLabels = false;
    private boolean hasLabelForSelected = false;
    private boolean pointsHaveDifferentColor;
    private LineChartData data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_statistics);
        initView();
        initEvent();
    }

    private void initEvent() {
        generateValues();

        generateData();
    }


    private void generateValues() {
        for (int i = 0; i < maxNumberOfLines; ++i) {
            for (int j = 0; j < numberOfPoints; ++j) {
                randomNumbersTab[i][j] = (float) Math.random() * 100f;
            }
        }
    }

    private void generateData() {

        List<Line> lines = new ArrayList<>();
        for (int i = 0; i < numberOfLines; ++i) {

            List<PointValue> values = new ArrayList<PointValue>();
            for (int j = 0; j < numberOfPoints; ++j) {
                values.add(new PointValue(j, randomNumbersTab[i][j]));
            }

            Line line = new Line(values);
            line.setColor(ChartUtils.COLORS[i]);
            line.setShape(ValueShape.CIRCLE); //折线图上每个数据点的形状  这里是圆形 （有三种 ：ValueShape.SQUARE  ValueShape.CIRCLE  ValueShape.DIAMOND）
            line.setCubic(true); //曲线是否平滑，即是曲线还是折线
            line.setFilled(true);//是否填充曲线的面积
            line.setHasLabels(false);//曲线的数据坐标是否加上备注
            line.setHasLabelsOnlyForSelected(hasLabelForSelected);//点击数据坐标提示数据（设置了这个line.setHasLabels(true);就无效）
            line.setHasLines(true);//是否用线显示。如果为false 则没有曲线只有点显示
            line.setHasPoints(false);//是否显示圆点 如果为false 则没有原点只有点显示（每个数据点都是个大的圆点）
            line.setStrokeWidth(1);
//            line.setHasGradientToTransparent(hasGradientToTransparent);
            if (pointsHaveDifferentColor) {
                line.setPointColor(ChartUtils.COLORS[(i + 1) % ChartUtils.COLORS.length]);
            }
            lines.add(line);
        }

        data = new LineChartData(lines);

        Axis axisX = new Axis();
        Axis axisY = new Axis().setHasLines(true);
        data.setAxisXBottom(axisX);
        data.setAxisYLeft(axisY);

        data.setBaseValue(Float.NEGATIVE_INFINITY);
        chart.setLineChartData(data);

    }


    private void initView() {


        chinaMap = findViewById(R.id.china_map);
        rcBiaozhu = findViewById(R.id.rc_biaozhu);
        rcBingzhong = findViewById(R.id.rc_bingzhong);
        tab = findViewById(R.id.tab);
        tab2 = findViewById(R.id.tab2);
        chart = findViewById(R.id.line_char);

        for (int i = 0; i < 10; i++) {
            list.add(i + "");
        }

        rcBiaozhu.setLayoutManager(new LinearLayoutManager(this));
        rcBiaozhu.setNestedScrollingEnabled(false);
        rcBiaozhu.setAdapter(new RankAdapter(R.layout.item_rank, list));


        rcBingzhong.setLayoutManager(new LinearLayoutManager(this));
        rcBingzhong.setNestedScrollingEnabled(false);
        rcBingzhong.setAdapter(new RankAdapter(R.layout.item_rank, list));


        chinaMap.setOnProvinceSelectedListener(new ChinaMapView.OnProvinceSelectedListener() {
            @Override
            public void onprovinceSelected(ChinaMapView.Area pArea) {
                showToast(pArea.name());
            }
        });
        chinaMap.setPaintColor(ChinaMapView.Area.HeBei, Color.rgb(141, 210, 255), true);
        chinaMap.setPaintColor(ChinaMapView.Area.GuangDong, Color.rgb(15, 136, 242), true);
        chinaMap.setPaintColor(ChinaMapView.Area.BeiJing, Color.rgb(46, 169, 248), true);
        chinaMap.setPaintColor(ChinaMapView.Area.SiChuan, Color.rgb(15, 136, 242), true);
        tabList.add(new StatisticsTabEnitty("年", 0, 0));
        tabList.add(new StatisticsTabEnitty("月", 0, 0));
        tabList.add(new StatisticsTabEnitty("日", 0, 0));
        tab.setTabData(tabList);
        tab.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                showToast(position + "");
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        String[] mTitles = {"样本", "有效样本"};
        tab2.setTabData(mTitles);
        tab2.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {

            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

}
